package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mapper.RevisitMapper;
import com.qf.myexception.ServiceException;
import com.qf.pojo.RevisitPatientVo;
import com.qf.pojo.RevisitPojo;
import com.qf.service.PatientService;
import com.qf.service.RevisitService;
import com.qf.utils.Constant;
import com.qf.utils.ResultMsg;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
public class RevisitServiceImpl extends ServiceImpl<RevisitMapper, RevisitPojo> implements RevisitService {
    @Autowired
    RevisitMapper revisitMapper;
    @Autowired
    PatientService patientService;

//    @Override
//    public ResultMsg findAll(int current, int pageSize) {
//        Page<RevisitPatientVo> page = new Page<>(current,pageSize);
//        QueryWrapper<RevisitPojo> wrapper = new QueryWrapper<>();
//        wrapper.eq("revisit_delete",0);
//        Page<RevisitPatientVo> page1=revisitMapper.getPageVo(page,wrapper);
//        if(page1!=null){
//            return ResultMsg.SUCCESS(page1);
//        }
//        throw  new ServiceException(201,"查询回访失败");
//    }

    @Override
    public ResultMsg findAll(Map<String, Object> maps) {
        Page<RevisitPatientVo> page = null;
        QueryWrapper<RevisitPojo> wrapper = new QueryWrapper<>();
        wrapper.eq("revisit_delete", 0);
        if (maps.size() != 0) {
            int current = 1;
            int pageSize = 20;
            Object o1 = maps.get("current");
            Object o2 = maps.get("pageSize");
            if (o1 != null && o2 != null && !o1.equals("") && !o2.equals("")) {
                current = Integer.parseInt(o1.toString());
                pageSize = Integer.parseInt(o2.toString());
            }
            page = new Page<>(current, pageSize);
            String startTime = (String) maps.get("startTime");
            String endTime = (String) maps.get("endTime");
            if (startTime != null && endTime != null && !startTime.equals("") && !endTime.equals("")) {
                wrapper.between("revisit_date", startTime + " 00:00:00", endTime + " 23:59:59");
            }
            Object status = maps.get("revisitStatus");
            if (status != null) {
                int status1 = Integer.parseInt(status.toString());
                wrapper.eq("revisit_status", status1);
            }
            Page<RevisitPatientVo> page1 = revisitMapper.getPageVo(page, wrapper);
            if (page1 != null) {
                return ResultMsg.SUCCESS(page1);
            }
            throw new ServiceException(201, "查询回访失败");
        }
        throw new ServiceException(201, "参数不能为空");
    }

    @Override
    public ResultMsg del(int revisitId) {
        if (revisitId <= 0) {
            throw new ServiceException(201, "revisitId参数不正确");
        }
        UpdateWrapper<RevisitPojo> wrapper = new UpdateWrapper<>();
        wrapper.eq("revisit_id", revisitId);
        wrapper.set("revisit_delete", 1);
        boolean update = this.update(wrapper);
        if (update) {
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201, "删除失败");
    }

    @Override
    public ResultMsg update(RevisitPojo revisit) {
        UpdateWrapper<RevisitPojo> wrapper = new UpdateWrapper<>();
        wrapper.eq("revisit_id", revisit.getRevisitId());
        if (this.getOne(wrapper) == null)
            return ResultMsg.FAILED(Constant.LZY + 1, "回访id revisit_id 不合法");
        boolean b = this.updateById(revisit);
        if (b) {
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201, "修改失败");
    }

    @Override
    public ResultMsg updateStatus(int revisitId) {
        UpdateWrapper<RevisitPojo> wrapper = new UpdateWrapper<>();
        wrapper.eq("revisit_id", revisitId);
        wrapper.set("revisit_status", 1);
        boolean update = this.update(wrapper);
        if (update) {
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201, "修改失败");
    }

//    @Override
//    public ResultMsg findByStatus(int status, int current, int pageSize) {
//        Page<RevisitPatientVo> page = new Page<>(current,pageSize);
//        QueryWrapper<RevisitPojo> wrapper = new QueryWrapper<>();
//        wrapper.eq("revisit_delete",0);
//        wrapper.eq("revisit_status",status);
//        Page<RevisitPatientVo> page1=revisitMapper.getPageVo(page,wrapper);
//        if(page1!=null){
//            return ResultMsg.SUCCESS(page1);
//        }
//        throw  new ServiceException(201,"查询回访失败");
//    }
//
//    @Override
//    public ResultMsg findByDate(String startTime, String endTime, int current, int pageSize) {
//        QueryWrapper<RevisitPojo> wrapper = new QueryWrapper<>();
//        wrapper.between("revisit_date",startTime,endTime);
//        wrapper.eq("revisit_delete",0);
//        Page<RevisitPatientVo> page = new Page<>(current,pageSize);
//        Page<RevisitPatientVo> page1=revisitMapper.getPageVo(page,wrapper);
//        if(page1!=null){
//            return ResultMsg.SUCCESS(page1);
//        }
//        throw  new ServiceException(201,"查询回访失败");
//
//    }

    @Override
    public ResultMsg add(RevisitPojo revisit) {
        if (revisit != null) {
            revisit.setRevisitDelete(0);
            boolean save = this.save(revisit);
            if (save) {
                return ResultMsg.SUCCESS();
            }
            throw new ServiceException(201, "新增预回访失败");
        }
        throw new ServiceException(201, "回访不能为空");
    }

    @Override
    public ResultMsg findOne(int revisitId) {
        QueryWrapper<RevisitPatientVo> wrapper = new QueryWrapper<>();
        wrapper.eq("revisit_id", revisitId);
        RevisitPatientVo one = revisitMapper.findOne(wrapper);
        if (one != null) {
            return ResultMsg.SUCCESS(one);
        }
        throw new ServiceException(201, "查询异常");
    }


}
